package org.smart4j.framework.demo.controller;


import org.smart4j.framework.annotation.Action;
import org.smart4j.framework.annotation.Controller;
import org.smart4j.framework.annotation.Inject;
import org.smart4j.framework.bean.Data;
import org.smart4j.framework.bean.Param;
import org.smart4j.framework.bean.View;
import org.smart4j.framework.demo.service.CustomerService;
import org.smart4j.framework.demo.vo.Customer;
import org.smart4j.framework.demo.vo.User;
import org.smart4j.framework.helper.ServletHelper;
import org.smart4j.framework.util.CastUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by DEAN on 2018/1/10.
 */
@Controller
public class WelcomeController {

    @Inject
    private CustomerService customerService;

    @Action("get:/welcomeTest")
    public View welcomeTest(Param param){
        System.out.println( "controller" + ServletHelper.getRequestPath());

        System.out.println("come in controller");
        long viewId = param.getLong("viewId");
        if(viewId==1){
            System.out.println("*****welcome.jsp*****");
            //当viewId为1时，返回welcome.jsp页面
            return new View("welcome.jsp").addModel("name","scout");
        }else{
            //其它情况返回index.jsp页面（通过重定向方式）
            System.out.println("*****index.jsp*****");
            return new View("/index.jsp");
        }
    }

    @Action("get:/test")
    public Data testData() {
        //目前框架不能解析JSON格式的传入参数
        //接受特定的传入参数：name和age
        System.out.println( "controller" + ServletHelper.getRequestPath());
        List<Customer > customers = customerService.getCustomerList();
        return new Data(customers);
    }
}
